import { Order } from 'order-model.js';
var order = new Order();
Page({
  data: {
    radioItems: [
      { name: '原路退回',  value: '0', 'checked': true },
      { name: '退回账户余额',  value: '1', checked: false}
    ],
    reason: [
      { name: '不想买了', value: '不想买了' },
      { name: '质量问题', value: '质量问题' },
      { name: '卖家发错货', value: '卖家发错货' },
      { name: '七天无理由', value: '七天无理由' },
      { name: '商品与页面图片不符', value: '商品与页面图片不符' },
      { name: '发票问题', value: '发票问题' },
      { name: '其它', value: '其它' },
    ],
    reasonIndex : 0,
    reasonText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = options.id ? options.id : '';
    this.data.status = options.status ? options.status : 1; //1发货前，2发货后，收货前
    if (!this.data.id){
      return false
    }
    this.setReason();
    this._loadData();
  },

  _loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var args = {
      order_id: this.data.id
    }
    order.getOrderDetails(args, (data) => {
      wx.hideLoading()
      var discount_price = Math.abs(data.discount_price);
      this.setData({
        orderData: data,
        discount_price: discount_price
      })
    })
  },
  // 申请退款
  _refundOrder: function (event) {
    var id = this.data.id;
    var radioItems = this.data.radioItems;
    var type = '';
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      if(radioItems[i].checked){
        type = radioItems[i].value;
      } 
    }
    if(!type || !id){
      return false;
    }
    var args = {
      order_id: id,
      type : type,
      note : this.data.reasonText
    }
    var that = this;
    order.refundOrder(args, (res) => {
      if (res.status == 1) {
        wx.showToast({
          title: '申请退款成功！',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateTo({
            //url: 'refund-success?entry=1',
            url: 'list',
          })
        }, 2000);
      } else {
        wx.showToast({
          title: '系统繁忙，请重试一下~',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //退款方式
  radioChange: function (e) {
    this.data.payMethod = e.detail.value;
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }
    this.setData({
      radioItems: radioItems
    });
  },
  //选择退货原因
  select:function(e){
    var index = e.currentTarget.dataset.index;
    this.data.reasonIndex = index;
    this.setReason();
    this.hiddenShare();
  },
  setReason:function(){
    var reasonIndex = this.data.reasonIndex;
    var reason = this.data.reason;
    reason.forEach(function (value, index) {
      if (index == reasonIndex) {
        reason[index].checked = true;
      } else {
        reason[index].checked = false;
      }
    });
    this.setData({
      reason,
      reasonText: this.data.reason[reasonIndex].value
    })
  },
  //点击选择原因按钮
  selectReason: function () {
    this.showShare();
  },
  /**
   * 展示选择原因弹框
   */
  showShare: function () {
    this.setData({
      showShare: true
    })
  },
  /**
   * 隐藏选择原因弹框
   */
  hiddenShare: function () {
    this.setData({
      showShare: false
    })
  },
})